其他
为什么 Java 中 1000==1000 为 false ?
Integer a = 1000, b = 1000;
System.out.println(a == b);//1
Integer c = 100, d = 100;
System.out.println(c == d);//2
false
true
Integer c = 100;
Integer i = Integer.valueOf(100);
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i
return IntegerCache.cache\[i + (-IntegerCache.low)\];
return new Integer(i);
}
Integer c = 100, d = 100;
System.out.println(c == d);
public static void main(String\[\] args) throws NoSuchFieldException, IllegalAccessException {
Class cache = Integer.class.getDeclaredClasses()\[0\]; //1
Field myCache = cache.getDeclaredField("cache"); //2
myCache.setAccessible(true);//3
Integer\[\] newCache = (Integer\[\]) myCache.get(cache); //4
newCache\[132\] = newCache\[133\]; //5
int a = 2;
int b = a + a;
System.out.printf("%d + %d = %d", a, a, b); //
}
往期推荐